package com.hz.house.controller;

import com.alibaba.fastjson.JSONObject;
import com.hz.house.bean.entity.CallPhoneBean;
import com.hz.house.bean.entity.CustomerBean;
import com.hz.house.dao.CallPhoneMapper;
import com.hz.house.dao.CustomerMapper;
import com.hz.house.dao.ToolsMapper;
import com.hz.house.service.SellManService;
import com.hz.house.utils.ConverUtils;
import com.hz.house.utils.EncodeUtils;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api("第三方回调")
@RestController
@RequestMapping("/callBack")
public class NotAuthController {
    @Autowired
    private SellManService sellManService;
    @Autowired
    private CustomerMapper userMapper;
    @Autowired
    private ToolsMapper toolsMapper;

    @ApiOperation(value = "电话加密,通话信息第三方回调")
    @GetMapping("/encodePhoneCallBack")
    public void encodePhoneCallBack(@RequestParam(value = "orderId", required = false) String orderId,
                                    @RequestParam(value = "eventType",required = false) String eventType,
                                    @RequestParam(value = "state", required = false) String state,
                                    @RequestParam(value = "fee_time", required = false) String fee_time,
                                    @RequestParam(value = "hold_time", required = false) String hold_time,
                                    @RequestParam(value = "start_time", required = false) String start_time,
                                    @RequestParam(value = "end_time", required = false) String end_time,
                                    @RequestParam(value = "params", required = false) String params,
                                    @RequestParam(value = "msg", required = false) String msg){
        log.info("orderId=" + orderId + ",eventType=" + eventType +",state="
                + state + ",fee_time=" + fee_time + ",holdTime="
                + hold_time + ",startTime=" + start_time + ",endTime="
                + end_time + ",params="+ params + ",msg=" + msg);
        this.sellManService.encodePhoneCallBack(orderId, eventType, state, fee_time, hold_time, start_time, end_time, params, msg);
    }
    @PostMapping("/callBack")
    public String callPhoneBack(@RequestParam(value = "from", required = false) String from,
                                @RequestParam(value = "to",required = false) String to,
                                @RequestParam(value = "encode_number", required = false) String encode_number,
                                @RequestParam(value = "state", required = false) int state,
                                @RequestParam(value = "start_time", required = false) String start_time,
                                @RequestParam(value = "end_time", required = false) String end_time,
                                @RequestParam(value = "params", required = false) String params,
                                @RequestParam(value = "time", required = false) String time,
                                @RequestParam("app_id") String app_id,
                                @RequestParam("sign") String sign){
        Map<String, Object> param = new HashMap<>();
        param.put("from", from);
        param.put("to", to);
        param.put("encode_number", encode_number);
        param.put("state", state);
        param.put("start_time", start_time);
        param.put("end_time", end_time);
        param.put("params", params);
        param.put("time", time);
        param.put("app_id", app_id);
        param.put("sign", sign);

        log.info(JSONObject.toJSONString(param));

        JSONObject json = new JSONObject();
        if (EncodeUtils.checkSign(param)){
            json.put("code", 0);
            json.put("msg", "success");
        }else {
            json.put("code", 1);
            json.put("msg", "sign error");
        }

        return json.toJSONString();
    }
    @PostMapping("/callPhoneNumber")
    public String callPhoneNumber(@RequestParam("encode_number") String encode_number,@RequestParam("app_id") String app_id, @RequestParam("sign") String sign){
        Map<String, Object> param = new HashMap<>();
        param.put("encode_number", encode_number);
        param.put("app_id", app_id);
        param.put("sign", sign);

        JSONObject json = new JSONObject();
        if (EncodeUtils.checkSign(param)){
            json.put("code", 0);
            json.put("msg", "success");
            JSONObject data = new JSONObject();
            json.put("data", data);

            data.put("to", "15828676169");
            data.put("params", "999");
            data.put("callback", "http://118.24.85.122:8088/phone/callBack");
        }else {
            json.put("code", 1);
            json.put("msg", "sign error");
        }
        return json.toJSONString();
    }

    @ApiOperation("矫正楼盘评论数")
    @PostMapping("/initBuildingPreseCount")
    public String initBuildingPreseCount(){
        long start = System.currentTimeMillis();
        List<Integer> bds = toolsMapper.getAllBuildingId();
        bds.forEach( (e) ->{
            if (e == 10003208){
                System.out.println("11111111111111");
            }
            List<Integer> ids = toolsMapper.selectBuildingCommentCount(e);
            int count = ids.size();
            String commIds = "";
            for (Integer id:ids){
                if (!commIds.isEmpty()) commIds +=",";
                commIds += id;
//                count += toolsMapper.selectBuildingCommentReplyCount(id);
            }
            if (!commIds.isEmpty()){
                count += toolsMapper.selectBuildingCommentReplyCountByIds(commIds);
            }
            toolsMapper.updateBuildingComments(e, count);
        });
        long used = System.currentTimeMillis() - start;
        return String.format("使用%s毫秒", String.valueOf(used));
    }

    @PostMapping("/testGetCustomerInfo")
    public String testGetCustomerInfo(){
        List<CustomerBean> customerBeans = userMapper.getUserByOpenId("orJEN5I0uUCeP-dI6srHn6--GL9E");
        return JSONObject.toJSONString(customerBeans);
    }
}
